class lib.resolver.TxtTimer
{
	private var syncArr:Array;
	private var _baseTime:Number;
	private var blockMin:Number;
	// private var _currentSyncNum:Number;

	private var MLFDispatcher;
	
	public function TxtTimer(dispatcher)
	{
		syncArr=new Array();
		MLFDispatcher=dispatcher;
	}

	public function close()
	{
		MLFDispatcher.removeEventListener("onPlayback", this);
	}

	public function forward()
	{
		MLFDispatcher.dispatch({type:"onTxtInit", param:blockMin});
		MLFDispatcher.addEventListener("onPlayback", this);
	}

	public function accessSyncByBlockNum(blockNum:Number):Number
	{
		if (blockNum>0)
		{
			return syncArr[blockNum-1];
		}
		else return 0;
	}

	public function seek(param:Number)
	{
		for (var i=0; i<syncArr[i]; i++)
		{
			// trace("param: "+param);
			// trace("sync: "+syncArr[i][0]);
			if (param<syncArr[i])
			{
				// trace("dispatch.onTxtSync: "+i);
				MLFDispatcher.dispatch({type:"onTxtSync", param:i});
				return;
			}
		}
	}

	private function onPlayback(obj:Object)
	{
		// trace("TxtTimer.onPlayback......");
		if (obj.param.type=="playback")
		{
			var processTime=obj.param.param-_baseTime;
			// trace("processTime: "+processTime);
			for (var i=0; i<syncArr.length-1; i++)
			{
				if (processTime==syncArr[i] || processTime==syncArr[i]-1)
				{
					MLFDispatcher.dispatch({type:"onTxtSync", param:i+1});
					return;
				}
			}
		}
	}

	public function set baseTime(bTime:Number)
	{
		_baseTime=bTime;
	}

	public function parseSync(syncXML)
	{
		// trace("parseSync......");
		for (var count=0; count<=syncXML.childNodes.length; count++)
		{
			if (syncXML.childNodes[count].nodeName.toLowerCase()=="sync")
			{
				buildSyncArr(syncXML.childNodes[count]);
				blockMin=Number(syncXML.childNodes[count].attributes.blockMin);
			}
		}
	}

	private function buildSyncArr(syncRoot)
	{
		// trace("buildSyncArr......");
		for (var count=0; count<=syncRoot.childNodes.length; count++)
		{
			if (syncRoot.childNodes[count].nodeName.toLowerCase()=="point")
			{
				var pointStr=syncRoot.childNodes[count].firstChild.nodeValue;
				// trace("pointStr: "+pointStr);
				var pointArr=pointStr.split(";");
				for (var i=0; i<pointArr.length; i++)
				{
					syncArr.push(pointArr[i]);
				}

				/*for (var i=0; i<syncArr.length; i++)
				{
					trace(i+": "+syncArr[i]);
				}*/
			}
		}
	}
}